<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/iconfont.css">
    <link rel="stylesheet" href="css/main.css">
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <script src="./libs/bootstrap/js/bootstrap.min.js"></script>
    <!-- 引入模板引擎js文件 -->
    <script src="./libs/template-web.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章类别管理
        </div>
        <div class="container-fluid common_con">
            <table class="table table-striped table-bordered table-hover mp20 category_table">
                <thead>
                    <tr>
                        <th>名称</th>
                        <th>Slug</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>奇趣事</td>
                        <td>funny</td>
                        <td class="text-center">
                            <a href="javascript:void(0)" data-toggle="modal" class=" btn btn-info btn-xs">编辑</a>
                            <a href="javascript:void(0)" class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="3" class="text-center">
                            <a href="" class="btn btn-success Prohibited" id="xinzengfenlei" data-toggle="modal"
                                data-target="#myModal" data-toggle="modal" data-target="#myModal">新增分类</a>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>

    <!-- 模态框 -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel">New message</h4>
                </div>
                <div class="modal-body">
                    <form>
                        <div class="form-group">
                            <label for="recipient-name" class="control-label">名称</label>
                            <input type="text" class="form-control article_name" id="recipient-name">
                        </div>
                        <div class="form-group">
                            <label for="message-text" class="control-label">Slug</label>
                            <input class="form-control article_type" id="message-text">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary btn-preserve">Send message</button>
                </div>
            </div>
        </div>
    </div>
</body>

</html>
<script type="text/html" id="tpl-category">
    {{each}}
    <tr>
        <td>{{$value.name}}</td>
        <td>{{$value.slug}}</td>
        <td class="text-center">
            <a href="" dataid = "{{$value.id}}" data-toggle="modal" data-target="#myModal" data-toggle="modal" class=" btn btn-info btn-xs Prohibited">编辑</a>
            <a href="" dataid = "{{$value.id}}" class="btn btn-danger btn-xs Prohibited btn-Delete">删除</a>
        </td>
    </tr>
    {{/each}}
</script>
<script src="./libs/https.js"></script>
<script>
    $(function () {
        // 1. 封装函数: 获得类别数据,渲染结构
        function loadData() {
            $.ajax({
                type: 'GET',
                url: 'http://localhost:8080/api/v1/admin/category/list',
                success: function (res) {
                    if (res.code !== 200) return console.log('获取失败');

                    // 将过去的数据渲染到页面
                    let datas = res.data;
                    // 将模板和数据揉和一起，返回结构字符串
                    let htmlStr = template('tpl-category', datas);
                    // 将结构渲染到页面
                    $('tbody').html(htmlStr);

                }
            })
        }
        loadData();


        // 禁止 a 标签跳转
        $('.Prohibited').on('load', function (e) {
            e.preventDefault();
        })

        // 判断区分模态框是由哪个按钮触发
        // 监听模态框的显示
        $('#myModal').on('show.bs.modal', function (e) {
            // console.log(e.relatedTarget); //获取触发模块框的元素对象
            if ($(e.relatedTarget).text() == '新增分类') {
                $('.modal-title').text('新增分类');
                $('.btn-preserve').text('新增');
                $('.article_name').val('');
                $('.article_type').val('');


            } else {
                $('.modal-title').text('编辑分类');
                $('.btn-preserve').text('编辑');
                // 将当前项数据，渲染到标签结构中
                window.eleid = $(e.relatedTarget).attr('dataid');
                $.ajax({
                    type: 'GET',
                    data: { id: eleid },
                    url: 'http://localhost:8080/api/v1/admin/category/search',
                    success: function (res) {
                        if (res.code === 200) {
                            // 将对应的数据。给模态框标签结构
                            $('.article_name').val(res.data[0].name);
                            $('.article_type').val(res.data[0].slug);
                        }
                    }
                })
            }
        })

        // 模态按钮添加事件，实现新增 和 修改功能
        $('.btn-preserve').on('click', function (e) {
            e.preventDefault();

            let name = $('#recipient-name').val().trim();
            let slug = $('#message-text').val().trim();


            // 封装函数，发起ajax 请求
            function RequestAjax(url, data, code) {
                $.ajax({
                    type: 'POST',
                    url: url,
                    data: data,
                    success: function (res) {
                        // 隐藏模态框,并刷新页面
                        if (res.code === code) {
                            $('#myModal').modal('hide');
                            loadData();
                        }
                    }
                })
            }

            // 新增功能
            if ($(this).text() == '新增') {
                // 发起ajax请求,新增数据
                RequestAjax(BigNew.category_add, { name, slug }, 201);
                // $.ajax({
                //     type: 'POST',
                //     url: 'http://localhost:8080/api/v1/admin/category/add',
                //     data: {
                //         name,
                //         slug
                //     },
                //     success: function (res) {
                //         // 隐藏模态框,并刷新页面
                //         if (res.code === 201) {
                //             $('#myModal').modal('hide');
                //             loadData();
                //         }
                //     }
                // })
            } else {
                // 发起ajax请求,修改文章
                RequestAjax(BigNew.category_edit, { id: window.eleid, name, slug }, 200);
                // $.ajax({
                //     type: 'POST',
                //     url: 'http://localhost:8080/api/v1/admin/category/edit',
                //     data: {
                //         id: window.eleid,
                //         name,
                //         slug
                //     },
                //     success: function (res) {
                //         console.log(res);
                //         // 隐藏模态框,并刷新页面
                //         if (res.code === 200) {
                //             $('#myModal').modal('hide');
                //             loadData();
                //         }
                //     }
                // })
            }
        })



        // 文章类别 删除模块(利用事件委托)
        $('tbody').on('click', '.btn-Delete', function (e) {
            e.preventDefault();

            // 点击删除按钮时，获取当前行的id
            let dataid = $(this).attr('dataid');
            $.ajax({
                type: 'POST',
                url: 'http://localhost:8080/api/v1/admin/category/delete',
                data: {
                    id: dataid
                },
                success: function (res) {
                    if (res.code === 204) {
                        loadData();
                    }
                }
            })
        })
    })
</script>